<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: Upgrade.php 289 2008-11-14 05:34:40Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Common.Upgrade.PortalUpgrade');
class Upgrade extends PortalUpgrade
{

	public function run()
	{
		parent::run();

		// From 1.0 there is no db changes

		$db = $this->DbConnection;
		// Check that the language portlet i installed
		$lprow = $db->createCommand("select * from tblportlets where name='LanguagePortlet'")->queryRow();
		if($lprow === null)
		{
			try
			{
				$db->createCommand("insert into tblportlets(name,displayname,active) values('LanguagePortlet','Change language','1')")->execute();
			}
			catch(Exception $e) {}
		}

		$iprow = $db->createCommand("select * from tblportlets where name='ImagePortlet'")->queryRow();
		if($iprow === null)
		{
			try
			{
				$db->createCommand("insert into tblportlets(name,displayname,active) values('ImagePortlet','Show image','1')")->execute();
			}
			catch(Exception $e) {}
		}

		// Add a new field to tblpages
		try
		{
			$db->createCommand("ALTER TABLE tblpages ADD COLUMN createdby INTEGER UNSIGNED NOT NULL DEFAULT 0")->execute();
		}
		catch(Exception $e){}

		// Fix the SimpleText portlet
		$strow = $db->createCommand("select * from tblportlets where name='SimpleText'")->queryRow();
		if($strow === null)
		{
			try
			{
				$db->createCommand("insert into tblportlets(name,displayname,active) values('SimpleTextPortlet','Simple text','1')")->execute();
			}
			catch(Exception $e) {}
		}
		else
		{
			try
			{
				$db->createCommand("update tblportlets set name='SimpleTextPortlet' where id=". $strow['id'])->execute();
			}
			catch(Exception $e) {}
		}

		// Update old reference to 'SimpleText'
		$strows = $db->createCommand("select * from tblpagecontent where portlet='SimpleText'")->execute();
		if($strows && count($strows) > 0)
		{
			foreach($strows as $strow)
			{
				$db->createCommand("update tblpagecontent set portlet='SimpleTextPortlet' where id={$strow['id']}")->execute();
			}
		}

		$this->UpdVersionNum('1.0.1');
	}
}
?>